"use strict";
const electron = require("electron");
const safeDOM = {
  append(parent, child) {
    if (!Array.from(parent.children).find((e) => e === child)) {
      parent.appendChild(child);
    }
  },
  remove(parent, child) {
    if (Array.from(parent.children).find((e) => e === child)) {
      parent.removeChild(child);
    }
  }
};
function useLoading() {
  const className = `loaders-css__square-spin`;
  const styleContent = `
      @keyframes square-spin {
        25% { transform: perspective(100px) rotateX(180deg) rotateY(0); }
        50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg); }
        75% { transform: perspective(100px) rotateX(0) rotateY(180deg); }
        100% { transform: perspective(100px) rotateX(0) rotateY(0); }
      }
      .${className} > div {
        animation-fill-mode: both;
        width: 50px;
        height: 50px;
        background: #fff;
        animation: square-spin 3s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
      }
      .app-loading-wrap {
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #282c34;
        z-index: 9;
      }`;
  const oStyle = document.createElement("style");
  const oDiv = document.createElement("div");
  oStyle.id = "app-loading-style";
  oStyle.innerHTML = styleContent;
  oDiv.className = "app-loading-wrap";
  oDiv.innerHTML = `<div class="${className}"><div></div></div>`;
  return {
    appendLoading() {
      safeDOM.append(document.head, oStyle);
      safeDOM.append(document.body, oDiv);
    },
    removeLoading() {
      safeDOM.remove(document.head, oStyle);
      safeDOM.remove(document.body, oDiv);
    }
  };
}
function domReady(condition = ["complete", "interactive"]) {
  return new Promise((resolve) => {
    if (condition.includes(document.readyState)) {
      resolve(true);
    } else {
      document.addEventListener("readystatechange", () => {
        if (condition.includes(document.readyState)) {
          resolve(true);
        }
      });
    }
  });
}
electron.contextBridge.exposeInMainWorld("appElectronAPI", {
  // 日志
  logger: (level, content) => electron.ipcRenderer.invoke("logger", level, content),
  appVersion: () => electron.ipcRenderer.invoke("appVersion"),
  appQuit: () => electron.ipcRenderer.invoke("appQuit"),
  appRestart: () => electron.ipcRenderer.invoke("appRestart"),
  openDevTools: () => electron.ipcRenderer.invoke("openDevTools"),
  toggleDevTools: () => electron.ipcRenderer.invoke("toggleDevTools"),
  miniWindow: () => electron.ipcRenderer.invoke("miniWindow"),
  maxWindow: () => electron.ipcRenderer.invoke("maxWindow"),
  scaleWindow: () => electron.ipcRenderer.invoke("scaleWindow"),
  closeWindow: () => electron.ipcRenderer.invoke("closeWindow"),
  hideWindow: () => electron.ipcRenderer.invoke("hideWindow"),
  activeWindow: () => electron.ipcRenderer.invoke("activeWindow"),
  forceCloseWindow: (cfg) => electron.ipcRenderer.invoke("forceCloseWindow", cfg),
  openLink: (url) => electron.ipcRenderer.invoke("openLink", url),
  getUserDataPath: () => electron.ipcRenderer.sendSync("getUserDataPath"),
  getTempPath: () => electron.ipcRenderer.sendSync("getTempPath"),
  getDownloadsPath: () => electron.ipcRenderer.sendSync("getDownloadsPath"),
  getLogsPath: () => electron.ipcRenderer.sendSync("getLogsPath"),
  getCrashPath: () => electron.ipcRenderer.sendSync("getCrashPath"),
  getAutoImport: () => electron.ipcRenderer.sendSync("getAutoImport"),
  getBuildVersion: () => electron.ipcRenderer.sendSync("getBuildVersion"),
  getArch: () => process.arch,
  getPlatform: () => process.platform,
  getVersionNode: () => process.versions.node,
  getVersionChrome: () => process.versions.chrome,
  getVersionElectron: () => process.versions.electron,
  install: (filePath) => electron.ipcRenderer.invoke("install", filePath),
  hotUpdate: (version, hotUrl, diffUrl) => electron.ipcRenderer.invoke("hotUpdate", version, hotUrl, diffUrl),
  openFile: (filePath) => electron.ipcRenderer.invoke("openFile", filePath),
  cancelQuit: () => electron.ipcRenderer.invoke("cancelQuit"),
  sendMessage: (...args) => electron.ipcRenderer.send(...args),
  initAutoImport: (taskList) => electron.ipcRenderer.invoke("initAutoImport", taskList),
  doCrash: () => electron.ipcRenderer.invoke("doCrash"),
  getRemoteText: (url, headers = {}) => electron.ipcRenderer.invoke("getRemoteText", url, headers),
  onMessage: (channel, func) => {
    const listener = (event, ...args) => func(...args);
    electron.ipcRenderer.on(channel, listener);
    return () => {
      electron.ipcRenderer.removeListener(channel, listener);
    };
  }
});
const { appendLoading, removeLoading } = useLoading();
domReady().then(appendLoading);
window.onmessage = (ev) => {
  ev.data.payload === "removeLoading" && removeLoading();
};
setTimeout(removeLoading, 2e3);
window.addEventListener("DOMContentLoaded", () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector);
    if (element) element.innerText = text;
  };
  const listVersion = ["chrome", "node", "electron"];
  listVersion.forEach((dependency) => {
    replaceText(`${dependency}-version`, process.versions[dependency]);
  });
});
